package com.qike.duola.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.common.collect.ImmutableMap;
import com.qike.duola.dto.ResourceAddDto;
import com.qike.duola.dto.ResourceDelDto;
import com.qike.duola.dto.ResourceEditDto;
import com.qike.duola.dto.ResourceListDto;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.shiro.LoginUser;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.LoginVo;
import com.qike.duola.vo.MenuListVo;
import com.qike.duola.vo.ResourceListByParentIdVo;
import com.qike.duola.vo.ResourceListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Api(description = "资源控制器")
@RestController
@RequestMapping("/resource")
@CrossOrigin
public class ResourceController {


    private static Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 获取用户所有菜单
     *
     * @return
     */
    @GetMapping("/AllMenuList")
    @ApiOperation("获取用户所有菜单")
    @ResponseBody
    public BaseResult AllMenuList() {
        try {
            ResourceListVo menuList = isysResourceService.getMenuList(null);
            return RetResponse.makeOKRsp(menuList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 资源管理列表
     *
     * @return
     */
    @GetMapping("/resourceList")
    @ApiOperation("资源管理列表")
    @ResponseBody
//    @RequiresPermissions("system:resource:*")
    public BaseResult resourceList(ResourceListDto resourceListDto) {
        try {
            ResourceListVo resourceList = isysResourceService.resourceList(resourceListDto);
            return RetResponse.makeOKRsp(resourceList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 资源管理列表 parentID不传获取所有父级资源 id获取某一条数据
     *
     * @return
     */
    @GetMapping("/resourceListByParentId")
    @ApiOperation("资源管理列表 parentID不传获取所有父级资源 id获取某一条数据")
    @ResponseBody
//    @RequiresPermissions("system:resource:*")
    public BaseResult resourceListByParentId(Long parentId,Long id) {
        try {
            List<ResourceListByParentIdVo> resourceList = isysResourceService.resourceListByParentId(parentId,id);
            return RetResponse.makeOKRsp(resourceList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 资源管理列表 parentID不传获取所有父级资源 id获取某一条数据
     *
     * @return
     */
    @GetMapping("/resourceListByParentIdAndName")
    @ApiOperation("资源管理列表 parentID 不传获取所有 name 模糊搜索")
    @ResponseBody
//    @RequiresPermissions("system:resource:*")
    public BaseResult resourceListByParentIdAndName(Long parentId,String name) {
        try {
            List<ResourceListByParentIdVo> resourceList = isysResourceService.resourceListByParentIdAndName(parentId,name);
            return RetResponse.makeOKRsp(resourceList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加资源
     *
     * @return
     */
    @PostMapping("/resourceAdd")
    @ApiOperation("添加资源")
    @ResponseBody
//    @RequiresPermissions("system:resource:create")
    public BaseResult resourceAdd(@RequestBody ResourceAddDto resourceListDto) {
        try {
            isysResourceService.resourceAdd(resourceListDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 删除资源
     *
     * @return
     */
    @PostMapping("/resourceDel")
    @ApiOperation("删除资源")
    @ResponseBody
//    @RequiresPermissions("system:resource:del")
    public BaseResult resourceDel(@RequestBody ResourceDelDto resourceDelDto) {
        try {
            isysResourceService.resourceDel(resourceDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 编辑资源
     *
     * @return
     */
    @PostMapping("/resourceEdit")
    @ApiOperation("编辑资源")
    @ResponseBody
//    @RequiresPermissions("system:resource:edit")
    public BaseResult resourceEdit(@RequestBody ResourceEditDto resourceEditDto) {
        try {
            isysResourceService.resourceEdit(resourceEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
